package com.and.xmppsdk;

import org.drl.xmpp.TalkClient;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Window;

import com.and.xmppsdk.adapter.FixedTabsAdapter;
import com.and.xmppsdk.adapter.HomeFtAdapter;
import com.and.xmppsdk.fragment.panel.ExitPanel;
import com.and.xmppsdk.view.FixedTabsView;

public class HomeActivity extends BaseActivity {

	TalkClient talk;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		initViewPager(3, 0xFFFFFFFF, 0xFF000000);
		mFixedTabs = (FixedTabsView) findViewById(R.id.fixed_tabs);
		mFixedTabsAdapter = new FixedTabsAdapter(this,titles);
		mFixedTabs.setAdapter(mFixedTabsAdapter);
		mFixedTabs.setViewPager(mPager);
	}
	private FixedTabsView mFixedTabs;
	private HomeFtAdapter mPagerAdapter;
	private FixedTabsAdapter mFixedTabsAdapter;
	private ViewPager mPager;
	String titles[]={"会话","好友","设置"};
	private void initViewPager(int pageCount, int backgroundColor, int textColor) {
		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new HomeFtAdapter(getSupportFragmentManager(),titles);
		mPager.setAdapter(mPagerAdapter);
		mPager.setCurrentItem(1);
		mPager.setPageMargin(1);
	}
	@Override
	protected void onReleaseMemory() {
		
	}


	private ExitPanel mExitPanel;

	private void showExitPanel() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Fragment prev = getSupportFragmentManager().findFragmentByTag(
				ExitPanel.TAG);
		if (prev != null) {
			ft.remove(prev);
		}
		ft.addToBackStack(null);

		mExitPanel = ExitPanel.getInstance();
		mExitPanel.show(ft, ExitPanel.TAG);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitPanel();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
